package com.coder520.sync_learning.demo;

import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

import static java.lang.Thread.currentThread;
import static java.lang.Thread.sleep;

public class BooleanLockTest {
    private final Lock lock=new Booleanlock();

    public void syncMethod() throws InterruptedException{
        lock.lock();
        try {
            int randomTime=(int)Math.random()*10;
            System.out.println(currentThread()+" get lock");
            TimeUnit.SECONDS.sleep(randomTime);
        }catch (InterruptedException e){

        }finally {
            lock.unlock();
        }
    }
    
    public static void main(String[] args){
        BooleanLockTest b=new BooleanLockTest();
        IntStream.range(0,10).mapToObj(i-> new Thread(() -> {
			try {
				b.syncMethod();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		})).forEach(Thread :: start);

    }
}

